package one.three.fortyNine;

public class QueueWithThreeStacks<Item> {
	Stack<Item> s1;
	Stack<Item> s2;
	Stack<Item> s3;
	private int n;

	public QueueWithThreeStacks() {
		s1 = new Stack<Item>();
		s2 = new Stack<Item>();
		s3 = new Stack<Item>();
		n = 0;
	}

	public void enqueue(Item item) {
		s1.push(item);
		n++;
	}

	public Item dequeue() {
		Item r = null;
		if (n > 0) {
			for (int i = 0; i < n - 1; i++) {
				s2.push(s1.pop());
			}
			r = s1.pop();
			for (int i = 0; i < n - 1; i++) {
				s1.push(s2.pop());
			}
			n--;
		}
		return r;
	}

	public static void main(String[] args) {
		QueueWithThreeStacks<String> q = new QueueWithThreeStacks<String>();
		q.enqueue("a");
		q.enqueue("b");
		q.enqueue("c");

		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
		System.out.println(q.dequeue());
	}

}
